package com.example.onlinemonitoring.controller;

import com.example.onlinemonitoring.dao.AppalarmMapper;
import com.example.onlinemonitoring.model.Appalarm;
import com.example.onlinemonitoring.model.common.MyResult;
import com.example.onlinemonitoring.model.common.pagebase;
import com.example.onlinemonitoring.query.QueryObject;
import com.example.onlinemonitoring.serviceImpl.AppalarmServiceImpl;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import com.example.onlinemonitoring.Util.LanguageManager;
import javax.servlet.http.HttpServletResponse;

import org.apache.ibatis.session.RowBounds;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.annotations.ApiOperation;
import tk.mybatis.mapper.entity.Example;
import io.swagger.annotations.Api;

@Api(value = "设备报警表", tags = { "设备报警接口" })
@RestController
@RequestMapping("/alarm")
public class AlarmController {
    @Autowired
    LanguageManager languageManager;
    @Autowired
    AppalarmServiceImpl appalarmimpl;

    @ApiOperation(value = "查询告警记录", notes = "查询告警记录")
    @ResponseBody
    @GetMapping("/selectAlarm")
    public MyResult<Appalarm> selectAlarm(@RequestParam(value = "deviceid", required = false) Integer deviceid,
            @RequestParam(value = "alarmlevel", required = false) Integer alarmlevel,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "pagecount", required = false) Integer pagecount,
            @RequestParam(value = "page", required = true) Integer page,
            @RequestParam(value = "limit", required = true) Integer limit) {
        QueryObject qo = new QueryObject(page, limit);
        Example example = new Example(Appalarm.class);
        example.orderBy("starttime").desc();
        Example.Criteria criteria = example.createCriteria();
        if (deviceid != null) {
            criteria.andEqualTo("deviceid", deviceid);
        }
        if (alarmlevel != null) {
            criteria.andEqualTo("alarmlevel", alarmlevel);
        }
        if (status != null) {
            criteria.andEqualTo("status", status);
        }
        return appalarmimpl.getList(qo, example);
    }

    @ApiOperation(value = "查询告警统计", notes = "查询告警统计")
    @ResponseBody
    @GetMapping("/selectCount")
    public MyResult<Integer> selectCount(@RequestParam(value = "deviceid", required = false) Integer deviceid,
            @RequestParam(value = "alarmlevel", required = false) Integer alarmlevel,
            @RequestParam(value = "status", required = false) Integer status) {
        Example example = new Example(Appalarm.class);
        Example.Criteria criteria = example.createCriteria();
        if (deviceid != null) {
            criteria.andEqualTo("deviceid", deviceid);
        }
        if (alarmlevel != null) {
            criteria.andEqualTo("alarmlevel", alarmlevel);
        }
        if (status != null) {
            criteria.andEqualTo("status", status);
        }
        return appalarmimpl.getCount(example);
    }

    @ApiOperation(value = "删除告警事件", notes = "删除告警事件")
    @ResponseBody
    @GetMapping("/clear")
    public MyResult<Integer> clear() {
        int res = appalarmimpl.Delete();
        return new MyResult<Integer>(0, languageManager.getLan().getDeleteSuccess());
    }

    /**
     * 导出Excel
     */
    @GetMapping("exportToExcel")
    public void exportToExcel(HttpServletResponse response,
            @RequestParam(value = "deviceid", required = false) Integer deviceid,
            @RequestParam(value = "alarmlevel", required = false) Integer alarmlevel,
            @RequestParam(value = "status", required = false) Integer status,
            @RequestParam(value = "maxrownum", required = false) Integer maxrownum) throws IOException {
        List<Appalarm> list = new ArrayList<>();
        Example example = new Example(Appalarm.class);
        example.orderBy("starttime").desc();
        Example.Criteria criteria = example.createCriteria();
        if (deviceid != null) {
            criteria.andEqualTo("deviceid", deviceid);
        }
        if (alarmlevel != null) {
            criteria.andEqualTo("alarmlevel", alarmlevel);
        }
        if (status != null) {
            criteria.andEqualTo("status", status);
        }
        if (maxrownum != null) {
            RowBounds rb = new RowBounds(0, maxrownum);
            list = appalarmimpl.selectByExampleAndRowBounds(example, rb);
        } else {
            list = appalarmimpl.selectByExample(example);
        }
        Workbook wb;
        if (maxrownum == 0) {
            wb = new XSSFWorkbook();
        } else {
            wb = appalarmimpl.writeToExcelByList(list);
        }
        OutputStream output = response.getOutputStream();
        String fileName = "data.xlsx";
        try {
            fileName = URLEncoder.encode(fileName, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        response.setHeader("Content-disposition",
                "attachment;filename=" + fileName + ";" + "filename*=utf-8''" + fileName);
        wb.write(output);
        output.close();
    }
}
